package com.ruoyi.common.utils;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.URLConstant;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 获取access_token工具类
 */
public class DingDingUtil {
    private static final Logger bizLogger = LoggerFactory.getLogger(DingDingUtil.class);

    public static String getToken(){
        try {
            DefaultDingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_GET_TOKKEN);
            OapiGettokenRequest request = new OapiGettokenRequest();

            request.setAppkey(Constants.APP_KEY);
            request.setAppsecret(Constants.APP_SECRET);
            request.setHttpMethod("GET");
            OapiGettokenResponse response = client.execute(request);
            return response.getAccessToken();
        } catch (ApiException e) {
            bizLogger.error("getAccessToken failed", e);
            throw new RuntimeException();
        }
    }
    public static Map<String, String> getUserInfo(String accessToken, String code) {
        try {
            DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_GET_USER_Id);
            OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
            req.setCode(code);
            OapiV2UserGetuserinfoResponse rsp = client.execute(req, accessToken);

            // 提取用户信息
            String name = rsp.getResult().getName();
            String userId = rsp.getResult().getUserid();

            Map<String, String> userInfo = new HashMap<>();
            userInfo.put("name", name);
            userInfo.put("userId", userId);

            return userInfo;
        } catch (ApiException e) {
            bizLogger.error("DingTalk 获取用户信息失败: {}", e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public static Map<String, String> getUserInfoDetail(String accessToken,String userId){
        try {
            DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_GET_USER_INFO);
            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
            req.setUserid(userId);
            req.setLanguage("zh_CN");
            OapiV2UserGetResponse rsp = client.execute(req, accessToken);
            Map<String, String> userInfo = new HashMap<>();
            userInfo.put("userName",rsp.getResult().getJobNumber());
            userInfo.put("postName",rsp.getResult().getTitle());
            return userInfo;
        } catch (ApiException e) {
            bizLogger.error("dingding getUserInfo failed {}", e.getMessage());
            throw new RuntimeException(e);
        }
    }
    public static List<Long> getDepartmentId(String userId, String accessToken) throws Exception {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
        OapiUserGetRequest req = new OapiUserGetRequest();
        req.setUserid(userId);
        req.setHttpMethod("GET");
        OapiUserGetResponse rsp = client.execute(req, accessToken);
        return rsp.getDepartment();
    }
    public static String getDepartmentName(String departmentId,String accessToken) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/get");
        OapiDepartmentGetRequest req = new OapiDepartmentGetRequest();
        req.setId(departmentId);
        req.setLang("zh_CN");
        req.setHttpMethod("GET");
        OapiDepartmentGetResponse rsp = client.execute(req, accessToken);
        return rsp.getName();
    }
}
